<?php
namespace app\admin\controller;

use think\Controller;
use think\Session;
use think\Request;
use app\admin\model\OrdersModel;

class Orders extends Common
{
	/**
	*	订单列表
	*/
	public function index()
	{
		$data = $this->request->param();
		$OrdersModel = new OrdersModel();
		// 商品列表
		$list = $OrdersModel->getOrders($data);
		$page = $list->render();
		$this->assign('page',$page);
		$this->assign('list',$list);

		$this->assign('o_code',isset($data['o_code']) ? $data['o_code'] : '');
		$this->assign('u_name',isset($data['u_name']) ? $data['u_name'] : '');
		$this->assign('o_status',isset($data['o_status']) ? $data['o_status'] : '');
		$this->assign('o_type',isset($data['o_type']) ? $data['o_type'] : '');
		$this->assign('o_company',isset($data['o_company']) ? $data['o_company'] : '');
		$this->assign('o_snumber',isset($data['o_snumber']) ? $data['o_snumber'] : '');
		return $this->fetch();
	}

	/**
	*	订单列表-商品信息 
	*/
	public function order_goods()
	{
		$data = $this->request->param();

		$OrdersModel = new OrdersModel();
		// 商品列表
		$list = $OrdersModel->getOrderGoods($data['gids']);
		$this->assign('list',$list);
		$this->assign('oid',$data['id']);
		return $this->fetch();
	}

	/**
	*	订单列表-商品规格信息 
	*/
	public function goodsinfo()
	{
		$data = $this->request->param();
		$OrdersModel = new OrdersModel();
		// 商品规格信息列表
		$list = $OrdersModel->getorderGoodsInfo($data['gid'],$data['oid']);
		$this->assign('list',$list);

		return $this->fetch();
	}

	/**
	*	获取选中会员对应的所有收货地址
	*/
	public function getUsersAddress()
	{
		$res = $this->request->param();
		if($res){
			$OrdersModel = new OrdersModel();
			$data = $OrdersModel->getUsersAddress($res['uid']);

			if($data){
				return returnJson($code=1,$msg='获取成功',$data);
			}else{
				return returnJson($code=0,$msg='获取成功',$data);
			}
		}
	}

	/**
	*	修改订单
	*/
	public function edit()
	{
		$data = $this->request->param();
		if($data){
			$OrdersModel = new OrdersModel();
			// 获取所有会员
			$users = $OrdersModel->getUsers();
			// 获取商品信息
			$goods = $OrdersModel->getGoods();

			// 获取收货地址
			$address = $OrdersModel->getUsersAddress($data['uid']);

			$list = $OrdersModel->getOne($data['id']);

			$this->assign('users',$users);
			$this->assign('goods',$goods);
			$this->assign('address',$address);
			$this->assign('list',$list);
		}
		return $this->fetch();
	}

	/**
	*	修改订单提交
	*/
	public function editPost()
	{
		if($this->request->isPost()){
			$data = $this->request->param();
			$OrdersModel = new OrdersModel();
			$result = $this->validate($data, 'OrdersValidate');

	        if ($result !== true) {

	            $this->error($result);
	        }
			$list = $OrdersModel->editOrders($data['id'],$data);
			$this->success('修改成功');
		}
	}

	/**
	*	删除订单
	*/
	public function delete()
	{
		$data = $this->request->param();
		if($data){
			$OrdersModel = new OrdersModel();
			$list = $OrdersModel->deleteOrders($data['id']);
			if($list){
				$this->success('删除成功',url('/admin/orders/index'));
			}else{
				$this->error('删除失败',url('/admin/orders/index'));
			}
		}
	}

}